package com.marvels.financial.loan.controller;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.funds.dto.FundDTO;
import com.marvels.financial.loan.api.FiancialLoanAuditApi;
import com.marvels.financial.loan.api.FinancialFundSourceApi;
import com.marvels.financial.loan.api.FinancialILoanAfterInfoApi;
import com.marvels.financial.loan.dto.ApprovalProcessConditionDTO;
import com.marvels.financial.loan.dto.LoanAfterInfoDTO;
import com.marvels.financial.loan.dto.McfeCustomerLoanContractDTO;
import com.marvels.financial.loan.dto.McfeLendingProcess;
import com.marvels.financial.loan.dto.McfeLendingProcessLog;
import com.marvels.financial.loan.dto.McfeSustainBankListDTO;
import com.marvels.financial.loan.dto.OverDueCustApiDTO;
import com.marvels.financial.loan.service.BankLimitRelevantApi;
import com.marvels.financial.loan.service.TimeStrikeBalanceApi;

@RestController
@RequestMapping("financial/loan/api")
public class FinancialProvideController {
	
	@Autowired
	private BankLimitRelevantApi bankLimitRelevantApi;
	
	@Autowired
	private FinancialFundSourceApi financialFundSourceService;
	
	@Autowired
	private FiancialLoanAuditApi fiancialLoanAuditApi;
	
	@Autowired
	private FinancialILoanAfterInfoApi loanAfterInfoApi;
	
	@Autowired
	private TimeStrikeBalanceApi timeStrikeBalanceApi;
	
	/**
	 * 查询资金源信息
	 * @param data
	 * @return
	 */
	@RequestMapping("/fundSourceInfo")
	@ResponseBody
    public List<FundDTO>  fundSourceInfo(@RequestBody FundDTO fundDTO) {
        return financialFundSourceService.fundSourceInfo(fundDTO);
    }
	
	/**
	 * 插入放款审核信息
	 * @param mcfeLendingProcess
	 * @return
	 */
	@RequestMapping("/insertLoanAuditInfo")
	public int insertLoanAuditInfo(@RequestBody McfeLendingProcess mcfeLendingProcess) {
		MarvelsLogUtil.getInstance().info("financial-loan-api-insertLoanAuditInfo : "+JSON.toJSONString(mcfeLendingProcess));
		return fiancialLoanAuditApi.insertLoanAuditInfo(mcfeLendingProcess);
	}


	/**
	 * 根据loanId 状态查询放款审核信息
	 * @param loanId
	 * @param status
	 * @return
	 */
	@RequestMapping("/selectByloanIdAndStatus")
	@ResponseBody
	public McfeLendingProcess selectByloanIdAndStatus(@RequestParam(name="loanId") String loanId,@RequestParam(name="status") String status) {
		return fiancialLoanAuditApi.selectByloanIdAndStatus(loanId, status);
	}

	/**
	 * 插入放款审核日志信息
	 * @param mcfeLendingProcessLog
	 * @return
	 */
	@RequestMapping("/insertLoanAuditLogInfo")
	public int insertLoanAuditLogInfo(@RequestBody McfeLendingProcessLog mcfeLendingProcessLog) {
		return fiancialLoanAuditApi.insertLoanAuditLogInfo(mcfeLendingProcessLog);
	}
	
	
	/**
	 * 贷后数据试算
	 * @param loanAfterInfoDTO
	 * @return
	 */
	@RequestMapping("/loanTrial")
	@ResponseBody
	public LoanAfterInfoDTO loanTrial(@RequestBody LoanAfterInfoDTO loanAfterInfoDTO){
		LoanAfterInfoDTO afterInfoDTO=null;
		try {
			afterInfoDTO=loanAfterInfoApi.loanTrial(loanAfterInfoDTO);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>loanId:"+loanAfterInfoDTO.getLoanId()+",贷后数据试算异常:",e);
		}
		return afterInfoDTO;
	}

	/*
	 * 根据loanid查询合同信息
	 */
	@RequestMapping("/selectContractByLoanId")
	@ResponseBody
	public McfeCustomerLoanContractDTO selectContractByLoanId(@RequestBody McfeCustomerLoanContractDTO dto){
		McfeCustomerLoanContractDTO contractDTO=loanAfterInfoApi.selectContractByLoanId(dto.getLoanId());
		return contractDTO;
	}
	
	
	/**
	 * 冲账
	 */
	@RequestMapping("/strikeBalance")
	@ResponseBody
	public void strikeBalance(){
		timeStrikeBalanceApi.excute();
	}
	/**
	 * 获取逾期客户列表
	 */
	@RequestMapping("/overdueCustList")
	@ResponseBody
	public MarvelsResultDTO overdueCustList(@RequestBody OverDueCustApiDTO overDueCustApiDto){
		Map<String,Object> result = null;
		try {
			result = loanAfterInfoApi.overdueCustList(overDueCustApiDto);
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch (Exception e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),result);
	}
	
	/**
	 * 获取逾期loanId列表
	 * @return
	 */
	@RequestMapping("/overdueLoanList")
	@ResponseBody
	public List<String> overdueLoanList(@RequestBody ApprovalProcessConditionDTO dto){
		return loanAfterInfoApi.overdueLoanList(dto);
	}

	
	
	
	@RequestMapping("selectSustainBankByBankName")
	@ResponseBody
	public MarvelsResultDTO selectSustainBankByBankName(@RequestBody McfeSustainBankListDTO record){
		try {
			List<McfeSustainBankListDTO> list = bankLimitRelevantApi.selectByBankName(record);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					list);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>通过银行名称查询银行支持列表失败:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>通过银行名称查询银行支持列表异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}
	}

	@RequestMapping("selectSustainBankList")
	@ResponseBody
	public MarvelsResultDTO selectSustainBankList(){
		try {
			List<McfeSustainBankListDTO> list = bankLimitRelevantApi.selectSustainBankList();
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					list);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>查询全部银行支持列表失败:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>查询全部银行支持列表异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}
	}
}
